"""Regular expression compatibility layer."""

from re import *

try:
    sub('_', ' ',  'foo_bar', flags=0)  # Optional 'flags' argument was
    subn('_', ' ', 'foo_bar', flags=0)  # added in Python 3.1.
    split('_',     'foo_bar', flags=0)

except TypeError:
    # Code below taken from Python 3.1 implementation.
    def sub(pattern, repl, string, count=0, flags=0):
        """Return the string obtained by replacing the leftmost
        non-overlapping occurrences of the pattern in string by the
        replacement repl.  repl can be either a string or a callable;
        if a string, backslash escapes in it are processed.  If it is
        a callable, it's passed the match object and must return
        a replacement string to be used."""
        return compile(pattern, flags).sub(repl, string, count)

    def subn(pattern, repl, string, count=0, flags=0):
        """Return a 2-tuple containing (new_string, number).
        new_string is the string obtained by replacing the leftmost
        non-overlapping occurrences of the pattern in the source
        string by the replacement repl.  number is the number of
        substitutions that were made. repl can be either a string or a
        callable; if a string, backslash escapes in it are processed.
        If it is a callable, it's passed the match object and must
        return a replacement string to be used."""
        return compile(pattern, flags).subn(repl, string, count)

    def split(pattern, string, maxsplit=0, flags=0):
        """Split the source string by the occurrences of the pattern,
        returning a list containing the resulting substrings."""
        return compile(pattern, flags).split(string, maxsplit)

